/* Filename: player.c
 * Description: 父子进程匿名管道，父写子读
 * Last modified: humble 20200504 00:44
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUFSIZE (1024)

static void usage(void);

int main(int argc, char **argv)
{
    int pd[2];
    pid_t pid;
    char buf[BUFSIZE];
    int len;

    if(argc != 1){
        fprintf(stderr, "argc !=1");
        usage();
        exit(1);
    }

    if(pipe(pd) < 0){
        perror("pipe()");
        exit(1);
    }

    pid = fork();
    if(pid < 0){
        perror("fork()");
        exit(1);
    }

    if(pid == 0){
        close(pd[1]);
        dup2(pd[0], 0);
        close(pd[0]);
        fd = open("/dev/null", O_RDWR);
        if(fd < 0){
            perror("open()");
            exit(1);
        }
        dup2(fd, 1);
        dup2(fd, 2);
        execl("/usr/bin/mpg123", "mpg123", "-", NULL);
        perror("execl()");
        exit(1);
    }
    else{ //parent
        close(pd[0]);
        //父进程从网上收数据，往管道中写
        close(pd[1]);
        wait(NULL);
        exit(0);
    }

    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
